package Dao;

import Bean.*;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import Dao.ClassDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CourseDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public ArrayList<BeanCourse> getCourses() throws Exception {

        return null;
    }
    public ArrayList<BeanCourse> getCoursesByClass(int id) throws Exception {
        String sql="select course_id,course_state,course_evaluation,weekday,span from course where class_id=?";
        ArrayList<BeanCourse> courses=new ArrayList<>();
        this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanTeacher>() {
            @Override
            public BeanTeacher extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                while(rs.next()) {
                    BeanCourse course=new BeanCourse();
                    course.setCourse_id(rs.getInt(1));
                    course.setCourse_state(rs.getBoolean(2));
                    course.setCourse_evaluation(rs.getString(3));
                    course.setWeekday(rs.getInt(4));
                    course.setSpan(rs.getInt(5));
                    courses.add(course);
                }
                return null;
            }},id);
        sql="select class_id,class_name,class_state,class_price,teacher_id,place_id,class_url from class where class_id=?";
        BeanClass myclass=new BeanClass();
        this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanClass>() {
            @Override
            public BeanClass extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                if(rs.next()) {
                    myclass.setClass_id(rs.getInt(1));
                    myclass.setClass_name(rs.getString(2));
                    myclass.setClass_state(rs.getBoolean(3));
                    myclass.setClass_price(rs.getDouble(4));
                    myclass.setTeacher_id(rs.getString(5));
                    myclass.setPlace_id(rs.getInt(6));
                    myclass.setClass_url(rs.getString(7));
                }
                return null;
            }},id);
        sql="select teacher_id,teacher_name,teacher_photo,teacher_introduction from teacher where teacher_id=?";
        BeanTeacher teacher=this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanTeacher>() {
            @Override
            public BeanTeacher extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                if(rs.next()) {
                    BeanTeacher teacher=new BeanTeacher();
                    teacher.setTeacher_id(rs.getString(1));
                    teacher.setTeacher_name(rs.getString(2));
                    teacher.setTeacher_photo(rs.getString(3));
                    teacher.setTeacher_introduction(rs.getString(4));
                    return teacher;
                }
                return null;
            }},myclass.getTeacher_id());
        sql="select place_id,place_name,place_photo,place_introduction from place where place_id=?";
        BeanPlace place=this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanPlace>() {
            @Override
            public BeanPlace extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                if(rs.next()) {
                    BeanPlace place=new BeanPlace();
                    place.setPlace_id(rs.getInt(1));
                    place.setPlace_name(rs.getString(2));
                    place.setPlace_photo(rs.getString(3));
                    place.setPlace_introduction(rs.getString(4));
                    return place;
                }
                return null;
            }},myclass.getPlace_id());

        myclass.setPlace(place);
        myclass.setTeacher(teacher);
        for(BeanCourse course:courses){
            course.setaClass(myclass);
        }
        return courses;
    }

}
